﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPI.Models;
using WebAPI.Models.Database;

namespace WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CategoryController : ControllerBase
    {
        //定义数据库上下文
        private readonly Store_2022Context _db;

        /// <summary>
        /// 构造方法 构造注入
        /// </summary>
        public CategoryController(Store_2022Context db)
        {
            _db = db;
        }
        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory(Category category)
        {
            _db.Categories.Add(category);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        /// <summary>
        /// 新增产品 自定请求类
        /// 站点名/api/Category/AddCategory2
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory2(AddCategory2 request)
        {
            //开始 业务逻辑

            //手动转换类 把请求类 转换 成 数据库模型类
            var category = new Category
            {              
                CateName = request.CateName,        
            };

            //数据库上下.操作的表名.操作的方法(参数)
            _db.Categories.Add(category);
            //保存更改 row影响条数
            var row = _db.SaveChanges();
            //数据库新增成功了
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        [HttpGet]
        public string AddCategory3(string CateName)
        {
            //把参数 转换 成商品模型类
            var category = new Category
            {
                CateName = CateName,
            };
            //数据库上下.操作的表名.操作的方法(参数)
            _db.Categories.Add(category);
            //保存更改 row影响条数
            var row = _db.SaveChanges();
            //数据库新增成功了
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        [HttpPost]
        public string UpdateCate(Category category)
        {
            var cate = _db.Categories.FirstOrDefault(x => x.Id == category.Id);
            if (cate == null)
                return "类型不存在";
            cate.CateName = category.CateName;
            _db.Categories.Update(cate);
            var row = _db.SaveChanges();
            if (row > 0)
                return "更新成功";
            return "更新失败";
        }
        [HttpPost]
        public string DeleteCate(int id)
        {
            var cate = _db.Categories.FirstOrDefault(x => x.Id==id);
            if (cate == null)
                return "类型不存在";
            _db.Categories.Remove(cate);
            var row = _db.SaveChanges();
            if (row > 0)
                return "删除成功";
            return "删除失败";
        }
        [HttpGet]
        public List<Category>GetCategories()
        {
            var list = _db.Categories.ToList();
            return list;
        }
        //[HttpGet]
        //public List<Category>GetCategories2(SearchCate request)
        //{
        //    var list = _db.Categories.AsQueryable();

        //}
    }
}
